package handler

import (
	"2302a/audio_podcast/api/request"
	"2302a/audio_podcast/api/resp"
	"2302a/audio_podcast/consts"
	"2302a/audio_podcast/internal/dao"
	"2302a/audio_podcast/internal/model"
	"2302a/audio_podcast/pkg"
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
)

func Login(c *gin.Context) {
	var req request.UserLogin
	err := c.ShouldBind(&req)
	if err != nil {
		resp.Error(c, 10000, "参数信息错误", err.Error())
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{Mobile: req.Mobile}, &user)
	if user.Id == 0 {
		resp.Error(c, 10002, "用户已存在", nil)
		return
	}
	token := pkg.CreateJwtToken(user.Id, 24*time.Hour, consts.JWT_KEY)
	resp.Success(c, "登录成功", token)
}

func UploadFile(c *gin.Context) {
	file, _ := c.FormFile("file")

	dst := "\\D:\\phpstudy_pro\\WWW\\" + file.Filename
	c.SaveUploadedFile(file, dst)
	resp.Success(c, "上传文件成功", fmt.Sprintf("http://localhost:%s", file.Filename))

}
